package cn.zzp.ggkt.result;

import cn.zzp.ggkt.enums.ResultCodeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author Administrator
 * @description: 统一返回结果
 * @date 2022/8/21 2:03
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "全局统一返回结果")
public class Result<T> {

    @ApiModelProperty(value = "返回码")
    private Integer code;

    @ApiModelProperty(value = "返回消息")
    private String message;

    @ApiModelProperty(value = "返回数据")
    private T data;

    /**
     * 定义操作成功方法
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.SUCCESS.getCode()); // 200
        result.setMessage(ResultCodeEnum.SUCCESS.getMessage()); // 成功
        if (data != null) {
            result.setData(data);
        }

        return result;
    }

    /**
     * 定义操作失败方法
     *
     * @param data
     * @param <T>
     * @return
     */
    public static <T> Result<T> fail(T data) {
        Result<T> result = new Result<>();
        result.setCode(ResultCodeEnum.FAIL.getCode()); // 201
        result.setMessage(ResultCodeEnum.FAIL.getMessage()); // 失败
        if (data != null) {
            result.setData(data);
        }

        return result;
    }

    /**
     * 自定义消息方法
     * @param message
     * @return
     */
    public Result<T> message(String message) {
        this.message = message;
        return this;
    }

    /**
     * 自定义返回值方法
     * @param code
     * @return
     */
    public Result<T> code(Integer code) {
        this.code = code;
        return this;
    }
}
